﻿using System;
using System.Windows.Forms;
using System.Windows.Input;
using ff14bot.Helpers;
using ff14bot.Managers;

namespace Magitek.Settings
{
    public partial class GeneralSettingsForm : Form
    {
        public GeneralSettingsForm()
        {
            InitializeComponent();
        }

        #region CBOItem from nomnomnom
        public class CboItem
        {
            public readonly int E;
            private readonly string _s;

            public CboItem(int pe, string ps)
            {
                E = pe;
                _s = ps;
            }

            public override string ToString()
            {
                return _s;
            }
        }
        #endregion
        #region Combobox ENUM - Required for ENUM in comboboxes from nomonom
        private static void SetComboBoxEnum(ComboBox cb, int e)
        {
            CboItem item;
            for (var i = 0; i < cb.Items.Count; i++)
            {
                item = (CboItem)cb.Items[i];
                if (item.E != e) continue;
                cb.SelectedIndex = i;
                return;
            }
            item = (CboItem)cb.Items[0];
            Logging.Write("[Magitek] Dialog Error: Combobox {0} does not have enum({1}) in list, defaulting to enum({2})",
                          cb.Name, e, item.E);
            cb.SelectedIndex = 0;
        }
        private static int GetComboBoxEnum(ComboBox cb)
        {
            var item = (CboItem)cb.Items[cb.SelectedIndex];
            return item.E;
        }
        #endregion

        private void GeneralSettingsForm_Closed(object sender, EventArgs e)
        {
            Magitek.WindowSettings.Save();
        }
        private void GeneralSettingsForm_Load(object sender, EventArgs e)
        {
            if (Magitek.WindowSettings.TargetAssistTank)
                cmbTargetStyle.Text = "Assist Tank";

            if (Magitek.WindowSettings.TargetFateEnemies)
                cmbTargetStyle.Text = "Fate Mobs";

            if (Magitek.WindowSettings.TargetClosestEnemy)
                cmbTargetStyle.Text = "Closest Enemy";

            if (Magitek.WindowSettings.TargetAssistDPS)
                cmbTargetStyle.Text = "Assist DPS";

            if (Magitek.WindowSettings.TargetLowestHealth)
                cmbTargetStyle.Text = "Lowest Health";

            txtTargetDistance.Text = Magitek.WindowSettings.TargetDistance.ToString();
            chkAutoStartCombat.Checked = Magitek.WindowSettings.TargetStartCombat;
            lstInterrupt.DataSource = Magitek.WindowSettings._interruptList;
            lstDontAOE.DataSource = Magitek.WindowSettings._dontaoeList;
            txtRestEnergy.Text = Magitek.WindowSettings.RestEnergy.ToString();
            txtRestHealth.Text = Magitek.WindowSettings.RestHealth.ToString();
            txtGeneralTargetstoAOE.Text = Magitek.WindowSettings.TargetsToAOE.ToString();
            chkSummonChocobo.Checked = Magitek.WindowSettings.SummonChocobo;
            chkAcceptRaise.Checked = Magitek.WindowSettings.acceptraise;
            txtFoodName.Text = Magitek.WindowSettings.foodname;
            chkUseFood.Checked = Magitek.WindowSettings.usefood;

            #region GUI code from nomnomnom
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.Alt, "Alt - Mod"));
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.Control, "Ctrl - Mod"));
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.Shift, "Shift - Mod"));
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.None, "Disable HK"));
            SetComboBoxEnum(ModifierKeyComboBox, (int)Magitek.WindowSettings.AOEModKeyChoice);

            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.None, "Modifier Only"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.XButton1, "Mouse button 4"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.XButton2, "Mouse button 5"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.D1, "1 (no numpad)"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.D2, "2 (no numpad)"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.D3, "3 (no numpad)"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.Q, "Q"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.E, "E"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.R, "R"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.Z, "Z"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.X, "X"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.C, "C"));
            SetComboBoxEnum(PauseKeyComboBox, (int)Magitek.WindowSettings.AOEKeyChoice);
            #endregion

        }
        private void chkSummonChocobo_CheckedChanged(object sender, EventArgs e)
        {
            Magitek.WindowSettings.SummonChocobo = chkSummonChocobo.Checked;
        }
        private void txtRestHealth_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtRestHealth.Text))
            {
                Magitek.WindowSettings.RestHealth = int.Parse(txtRestHealth.Text);
            }          
        }
        private void txtRestEnergy_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtRestEnergy.Text))
            {
                Magitek.WindowSettings.RestEnergy = int.Parse(txtRestEnergy.Text);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Magitek.WindowSettings.Save();
            this.Close();
        }
        private void txtGeneralTargetstoAOE_TextChanged(object sender, EventArgs e)
        {
            Magitek.WindowSettings.TargetsToAOE = int.Parse(txtGeneralTargetstoAOE.Text);
        }
        private void btnAddInterrupt_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtAddInterrupt.Text))
            {
                Magitek.WindowSettings._interruptList.Add(txtAddInterrupt.Text);
                txtAddInterrupt.Clear();
            }
        }
        private void btnAddDontAOE_Click(object sender, EventArgs e)
        {
                Magitek.WindowSettings._dontaoeList.Add(txtAddDontAOE.Text);
                txtAddDontAOE.Clear();

        }
        private void btnRemoveInterrupt_Click(object sender, EventArgs e)
        {
            if (lstInterrupt.SelectedItems.Count > 0)
            {
                Magitek.WindowSettings._interruptList.Remove((string) lstInterrupt.SelectedValue);
            }
        }
        private void btnRemoveDontAOE_Click(object sender, EventArgs e)
        {
            if (lstDontAOE.SelectedItems.Count > 0)
            {
                Magitek.WindowSettings._dontaoeList.Remove((string) lstDontAOE.SelectedValue);
            }
        }
        private void txtTargetDistance_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtTargetDistance.Text))
            {
                Magitek.WindowSettings.TargetDistance = int.Parse(txtTargetDistance.Text);
            }
        }
        private void cmbTargetStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbTargetStyle.Text == "Assist Tank")
            {
                Magitek.WindowSettings.TargetAssistTank = true;
                Magitek.WindowSettings.TargetFateEnemies = false;
                Magitek.WindowSettings.TargetClosestEnemy = false;
                Magitek.WindowSettings.TargetAssistDPS = false;
            }

            if (cmbTargetStyle.Text == "Fate Mobs")
            {
                Magitek.WindowSettings.TargetAssistTank = false;
                Magitek.WindowSettings.TargetFateEnemies = true;
                Magitek.WindowSettings.TargetClosestEnemy = false;
                Magitek.WindowSettings.TargetAssistDPS = false;
                Magitek.WindowSettings.TargetLowestHealth = false;
            }

            if (cmbTargetStyle.Text == "Closest Enemy")
            {
                Magitek.WindowSettings.TargetAssistTank = false;
                Magitek.WindowSettings.TargetFateEnemies = false;
                Magitek.WindowSettings.TargetClosestEnemy = true;
                Magitek.WindowSettings.TargetAssistDPS = false;
                Magitek.WindowSettings.TargetLowestHealth = false;
            }

            if (cmbTargetStyle.Text == "Assist DPS")
            {
                Magitek.WindowSettings.TargetAssistTank = false;
                Magitek.WindowSettings.TargetFateEnemies = false;
                Magitek.WindowSettings.TargetClosestEnemy = false;
                Magitek.WindowSettings.TargetAssistDPS = true;
                Magitek.WindowSettings.TargetLowestHealth = false;
            }

            if (cmbTargetStyle.Text == "Lowest Health")
            {
                Magitek.WindowSettings.TargetAssistTank = false;
                Magitek.WindowSettings.TargetFateEnemies = false;
                Magitek.WindowSettings.TargetClosestEnemy = false;
                Magitek.WindowSettings.TargetAssistDPS = false;
                Magitek.WindowSettings.TargetLowestHealth = true;
            }
        }
        private void chkAutoStartCombat_CheckedChanged(object sender, EventArgs e)
        {
            Magitek.WindowSettings.TargetStartCombat = chkAutoStartCombat.Checked;
        }
        private void chkAcceptRaise_CheckedChanged(object sender, EventArgs e)
        {
            Magitek.WindowSettings.acceptraise = chkAcceptRaise.Checked;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtFoodName.Text))
            {
                Magitek.WindowSettings.foodname = txtFoodName.Text;
            }
        }
        private void chkUseFood_CheckedChanged(object sender, EventArgs e)
        {
            Magitek.WindowSettings.usefood = chkUseFood.Checked;
        }
        private void ModifierKeyComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Magitek.WindowSettings.AOEModKeyChoice = (ModifierKeys)GetComboBoxEnum(ModifierKeyComboBox);
            RemoveHotkeys();
            RegisterHotkeys();
        }
        public void RemoveHotkeys()
        {
            HotkeyManager.Unregister("AOE Key");
        }
        public void RegisterHotkeys()
        {
            HotkeyManager.Register("AOE Key", Magitek.WindowSettings.AOEKeyChoice, Magitek.WindowSettings.AOEModKeyChoice, hk => { Magitek.WindowSettings.AOEEnabled = !Magitek.WindowSettings.AOEEnabled; Logging.Write(Magitek.WindowSettings.AOEEnabled ? "[Magitek] AOE Mode Enabled" : "[Magitek] AOE Mode Disabled"); });
        }
        private void PauseKeyComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MagitekSettings.Instance.AOEKeyChoice = (Keys)GetComboBoxEnum(PauseKeyComboBox);
            RemoveHotkeys();
            RegisterHotkeys();
        }
    }
}
